package com.teragadgets.android.comic.feed;

import java.io.IOException;
import java.util.Vector;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.teragadgets.android.comic.datamodel.Comic;

public class ComicFeedParser implements ContentHandler {

	private static String feedURL;
	private static Vector<Comic> comics;
	private static boolean reSync;

	public ComicFeedParser(String feedURL) {
		if (ComicFeedParser.feedURL == null || !ComicFeedParser.feedURL.equals(feedURL)) {
			ComicFeedParser.feedURL = feedURL;
			reSync = true;
		} else {
			reSync = false;
		}
	}

	public Vector<Comic> getComics() throws ClientProtocolException,
			IOException, ParserConfigurationException, SAXException {

		if(!reSync)
			return comics;
		
		comics = new Vector<Comic>();

		// Get XML list of comics
		HttpGet httpget = new HttpGet(feedURL);
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();

		// Create an XML parser for it
		SAXParserFactory spf = SAXParserFactory.newInstance();
		SAXParser sp = spf.newSAXParser();
		XMLReader parser = sp.getXMLReader();

		// Use this class to parse the content and store the results into the
		// comics variable
		parser.setContentHandler(this);
		parser.parse(new InputSource(entity.getContent()));
		return comics;
	}

	@Override
	public void endPrefixMapping(String arg0) throws SAXException {
	}

	@Override
	public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
			throws SAXException {
	}

	@Override
	public void processingInstruction(String arg0, String arg1)
			throws SAXException {
	}

	@Override
	public void setDocumentLocator(Locator arg0) {
	}

	@Override
	public void skippedEntity(String arg0) throws SAXException {
	}

	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		if (localName.equals("comic")) {
			String comicImage = atts.getValue("url");
			String comicTitle = atts.getValue("name");
			String comicAltText = atts.getValue("alt");
			int comicNumber = Integer.parseInt(atts.getValue("number"));
			comics.add(new Comic(comicTitle, comicImage, comicAltText, comicNumber));
		}

	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
	}

	@Override
	public void endDocument() throws SAXException {

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
	}

	@Override
	public void startPrefixMapping(String arg0, String arg1)
			throws SAXException {
	}

}
